import 'package:application_with_bilibili/widget/MyDialog.dart';
import 'package:flutter/material.dart';
import 'dart:developer';
import 'package:fluttertoast/fluttertoast.dart';

class MountainPage extends StatefulWidget {
  const MountainPage({super.key});

  @override
  State<MountainPage> createState() => _MountainPageState();
}

class _MountainPageState extends State<MountainPage> {
  // 简单的提示弹框
  void _alertDialog() async {
    var result = await showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("提示"),
          content: const Text("确定？？？"),
          actions: [
            TextButton(
              onPressed: () {
                // 弹框消失
                Navigator.pop(context, "取消");
              },
              child: const Text("取消"),
            ),
            TextButton(
              onPressed: () {
                Navigator.pop(context, "确定");
              },
              child: const Text("确定"),
            ),
          ],
        );
      },
    );
    if (result != null) {
      log(result);
    }
  }

  // 带选项的弹框
  void _simpleDialog() async {
    var result = showDialog(
      // 点击背景弹框是否消失
      barrierDismissible: true,
      context: context,
      builder: (context) {
        return SimpleDialog(
          title: const Text("你要去哪座山"),
          // 选项
          children: [
            SimpleDialogOption(
              child: const Text("黄山"),
              onPressed: () => Navigator.pop(context, "黄山"),
            ),
            SimpleDialogOption(
              child: const Text("泰山"),
              onPressed: () => Navigator.pop(context, "泰山"),
            ),
            SimpleDialogOption(
              child: const Text("华山"),
              onPressed: () => Navigator.pop(context, "华山"),
            ),
            SimpleDialogOption(
              child: const Text("衡山"),
              onPressed: () => Navigator.pop(context, "衡山"),
            ),
            SimpleDialogOption(
              child: const Text("嵩山"),
              onPressed: () => Navigator.pop(context, "嵩山"),
            ),
          ],
        );
      },
    );
    result.then(
      (value) => {
        if (value != null)
          {
            log(value),
          }
      },
    );
  }

  // 底部弹出框
  void _showModalBottomSheet() async {
    var result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return Column(
          children: [
            ListTile(
              title: const Text("黄山"),
              onTap: () => Navigator.pop(context, "黄山"),
            ),
            ListTile(
              title: const Text("泰山"),
              onTap: () => Navigator.pop(context, "泰山"),
            ),
            ListTile(
              title: const Text("华山"),
              onTap: () => Navigator.pop(context, "华山"),
            ),
            ListTile(
              title: const Text("衡山"),
              onTap: () => Navigator.pop(context, "衡山"),
            ),
            ListTile(
              title: const Text("嵩山"),
              onTap: () => Navigator.pop(context, "嵩山"),
            ),
          ],
        );
      },
    );
    if (result != null) {
      log(result);
    }
  }

  void _toast() {
    Fluttertoast.showToast(
        msg: "发生了不得了的事情",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.TOP,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.white,
        textColor: Colors.black,
        fontSize: 16.0);
  }

  void _myDialog() {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return Mydialog(
          title: "了不起的事情",
          content: "今天拉了三次屎",
          onTap: () => {Navigator.pop(context)},
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {
              _alertDialog();
            },
            child: const Text("alert弹出框-AlertDialog"),
          ),
          ElevatedButton(
            onPressed: () {
              _simpleDialog();
            },
            child: const Text("select弹出框-SimpleDialog"),
          ),
          ElevatedButton(
            onPressed: () {
              _showModalBottomSheet();
            },
            child: const Text("ActionSheet底部弹出框-ShowModalBottomSheet"),
          ),
          ElevatedButton(
            onPressed: () {
              _toast();
            },
            child: const Text("Toast"),
          ),
          ElevatedButton(
            onPressed: () {
              _myDialog();
            },
            child: const Text("MyDialog"),
          ),
        ],
      ),
    );
  }
}
